# -*- coding:utf-8 -*-
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
from Crypto.Random import get_random_bytes
import base64
import os

send = 1


AES_CBC_key = [b'pJyjnF5WXVmPuNKT', b'1weqkd4taPRvcAWS', b'ghH7puQz9OKxnwDB', b'OuYgslc4tz69rjm2', b'yxTwhMgN5vJj2aUr', b'zLVrPckDC8fFgWhy', b'fsenqu8RLv9yFkcA', b'E9niRTJMempFkvOK', b'8U29eKB6ntL4pz1D', b'GYoNSzh8e1n0Uvmy']
AES_CBC_key_decrypt = AES_CBC_key[::-1]
def encrypt_AES_CBC(plaintext, key):
    iv = get_random_bytes(AES.block_size)
    cipher = AES.new(key, AES.MODE_CBC, iv)
    ciphertext_bytes = cipher.encrypt(pad(plaintext.encode(), AES.block_size))
    return base64.b64encode(iv + ciphertext_bytes).decode('utf-8')

def decrypt_AES_CBC(ciphertext, key):
    ciphertext_bytes = base64.b64decode(ciphertext)
    iv = ciphertext_bytes[:AES.block_size]
    cipher = AES.new(key, AES.MODE_CBC, iv)
    decrypted_bytes = cipher.decrypt(ciphertext_bytes[AES.block_size:])
    return unpad(decrypted_bytes, AES.block_size).decode('utf-8')
#加密内容
def Hypixice_sign_encrypt(text):
    encrypt_text = text
    for i in AES_CBC_key:
        encrypt_text = encrypt_AES_CBC(encrypt_text,i)
    for _ in range(send):
        encrypt_text = base64.b64encode(encrypt_text.encode('utf-8')).decode('utf-8')
    encrypt_text = "!HypLockersign!" + encrypt_text
    return encrypt_text

#解密内容
def Hypixice_sign_decrypt(text):
    decrypt_text = str(text)
    if "!HypLockersign!" in text:
        decrypt_text = decrypt_text.replace("!HypLockersign!","")
        for _ in range(send):
            decrypt_text = base64.b64decode(decrypt_text.encode('utf-8')).decode('utf-8')
        for i in AES_CBC_key_decrypt:
            decrypt_text = decrypt_AES_CBC(decrypt_text, i)
    else:
        for _ in range(send):
            decrypt_text = base64.b64decode(decrypt_text.encode('utf-8')).decode('utf-8')
        for i in AES_CBC_key_decrypt:
            decrypt_text = decrypt_AES_CBC(decrypt_text, i)
    return decrypt_text


def out(text):
    send = int(text)
    print(f"完成！[{send}]")
    return send  # 添加返回值

def menu():
    global send
    os.system("cls")
    while True:
        x=input(f"========Hypixice Locker 演示版 [1.0 Pre]=======\n\n[1] 加密\n[2] 解密 \n[3] 设置重复加密次数[默认1,目前{send}]\n>")
        if x == "1":
            os.system("cls")
            text = input("请输入要加密的内容: ")
            print("加密中...")
            encrypted_text = Hypixice_sign_encrypt(text)
            os.system("cls")
            print(f"结果: {encrypted_text}\n")
            input("按回车返回")
            menu()
        elif x == "2":
            os.system("cls")
            text = input("请输入要解密的内容: ")
            print("解密中...")
            decrypted_text = Hypixice_sign_decrypt(text)
            os.system("cls")
            print(f"结果: {decrypted_text}\n")
            input("按回车返回")
            menu()
        elif x == "3":
            os.system("cls")
            text = input("请输入设置参数(整数,>0): ")
            send = out(text)  # 在这里使用返回值
            os.system("cls")
            print(f"完成！[{send}]")
            input("按回车返回")
            menu()


        else:
            break

menu()
